package com.wavehi.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @Author WaveHi
 * @Date 25/9/2021 下午 8:54
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Get session for database.
 */
public class MyBatisUtils {
    private MyBatisUtils() {}

    //接收数据库会话工厂
    private static SqlSessionFactory ssf;
    //获取会话工厂
    static {
        //获取配置信息读取流
        InputStream in = MyBatisUtils.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        //获取会话工厂构建器
        SqlSessionFactoryBuilder ssbf = new SqlSessionFactoryBuilder();
        //构建会话工厂
        ssf = ssbf.build(in);
    }

    /**
     * 获取数据库会话
     * @return 返回一个会话对象
     */
    public static SqlSession getSession() {
        return ssf.openSession();
    }

    /**
     * 获取会话工厂
     * @return 返回一个工厂对象
     */
    public static SqlSessionFactory getSsf() {
        return ssf;
    }
}
